/* MIT License
 *
 * Copyright (c) 2012 Reuben Smith, Miami University
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package org.droidlike.vsig;


/**
 * Commonly used functions and types.
 * 
 * @author smithre5
 */
public class Util
{
    /**
     * Returns the larger of two numbers.
     * 
     * @param <T> the type of the compared numbers
     * @param a the first number compared
     * @param b the second number compared
     * @return the number that is larger
     */
    public static <T extends Number & Comparable<? super T>> T max(T a, T b)
    {
        if (a.compareTo(b) >= 0) {
            return a;
        }
        
        return b;
    }
    
    
    /**
     * Returns the smaller of two numbers.
     * 
     * @param <T> the type of the compared numbers
     * @param a the first number compared
     * @param b the second number compared
     * @return the number that is smaller
     */
    public static <T extends Number & Comparable<? super T>> T min(T a, T b)
    {
        if (a.compareTo(b) <= 0) {
            return a;
        }
        
        return b;
    }
    
    
    /**
     * Returns a number clamped between two given numbers.
     * 
     * @param <T> the type of the numbers
     * @param value the initial value
     * @param min the range minimum value
     * @param max the range maximum value
     * @return value if within the range; otherwise, min or max is returned
     */
    public static <T extends Number & Comparable<? super T>> T clamp(T value, T min, T max)
    {
        return Util.max(min, Util.min(max, value));
    }
    
    
    /**
     * Returns whether or not a number is inside the inclusive range given.
     * 
     * @param <T> the type of the numbers
     * @param value the value checked
     * @param min the minimum range extent
     * @param max the maximum range extent
     * @return true iff in range
     */
    public static <T extends Number & Comparable<? super T>> boolean inRange(T value, T min, T max)
    {
        return Util.clamp(value, min, max) == value;
    }
    
    
    /**
     * Checks if a character sequence is blank. Blank is defined as null or empty.
     * 
     * @param seq the sequence to check
     * @return true if blank; false otherwise
     */
    public static boolean isBlank(CharSequence seq)
    {
        if (seq == null || seq.length() == 0) {
            return true;
        }
        
        return false;
    }
}

